rm -rf build/*
cp -R app/third build/

for page in `ls app/*.{html,php}`; do
  page=`basename $page`
  page=${page%%.*}

  if [ -f app/$page.html ]; then
    awk "!/$page\/config.js/" app/$page.html > build/$page.html
  elif [ -f app/$page.php ]; then
    cd app
    php -f $page.php > ../build/$page.pre
    awk "!/$page\/config.js/" ../build/$page.pre > ../build/$page.html
    rm -rf ../build/$page.pre
    cd ..
  fi

  if [ -e app/$page/img ];then
    cp -R app/$page/img build/$page/img
  fi

  lessc app/$page/index.less > app/$page/index.css
  r.js -o app/$page/build.js
done

tar czf build-`date +"%Y-%m-%d"`.tar.gz build
